package reflex_and_annotation.c_get_methods;

import reflex_and_annotation.entity.User;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

public class Test {

    public static void main(String[] args) {
        method4();
    }

    private static void method1() {
        Class clazz = getClazz();
        // 获取所有的公共方法（包含父类的方法）
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            System.out.println(method.getName() + "<==>"
                    + method.getDefaultValue() + "<==>"
                    + method.getReturnType() + "<==>"
                    + method.getParameterCount() + "<==>"
                    + Arrays.asList(method.getParameterTypes()) + "<==>"
                    + Arrays.asList(method.getTypeParameters()));
        }
    }

    private static void method2() {
        Class clazz = getClazz();
        // 获取所有方法（不包含父类的方法）
        Method[] methods = clazz.getDeclaredMethods();
        for (Method method : methods) {
            System.out.println(method.getName() + "<==>"
                    + method.getDefaultValue() + "<==>"
                    + method.getReturnType() + "<==>"
                    + method.getParameterCount() + "<==>"
                    + Arrays.asList(method.getParameterTypes()) + "<==>"
                    + Arrays.asList(method.getTypeParameters()));
        }
    }
    /**
     * @Author WLJ
     * @Description //TODO 通过方法名获取方法，并执行
     * @Date 16:35 2023/8/29
     * @Param []
     * @return void
     **/
    private static void method3() {
        Class clazz = getClazz();
        User user = new User();
        try {
            // 公共无参方法
            Method pub = clazz.getMethod("pub");
            // 不是静态方法就必须传对象
//            pub.invoke(null);
            pub.invoke(user);
            System.out.println("=====");
            // 公共的有一个参数的方法
            Method pub1 = clazz.getMethod("pub", int.class);
            pub1.invoke(user, 0);
            System.out.println("=====");
            Method pub2 = clazz.getMethod("pub", int.class, String.class);
            Object obj1 = pub2.invoke(user, 1, "字符串");
            System.out.println(obj1);
            user.pubs = "返回参数";
            obj1 = pub2.invoke(user, 2, "字符串1");
            System.out.println(obj1);
            System.out.println("=====");
            // 获取公共的静态方法
            Method pubStatic = clazz.getMethod("pubStatic");
            pubStatic.invoke(null);
            System.out.println("=====");
            // 获取私有的静态方法
            Method priStatic = clazz.getDeclaredMethod("priStatic");
            priStatic.invoke(null);
            System.out.println("=====");
            // 获取私有方法
            Method pri = clazz.getDeclaredMethod("pri");
            // 私有方法执行前必须要设置允许执行
            pri.setAccessible(true);
            pri.invoke(user);
            System.out.println("=====");
            // 获取父类的公共方法
            Method fatherPubMethod = clazz.getMethod("fatherPubMethod");
            fatherPubMethod.invoke(user);
            // 获取父类的私有方法（报错：方法找不到，method2就说明了问题）
            Method fatherPriMethod = clazz.getDeclaredMethod("fatherPriMethod");
            fatherPriMethod.setAccessible(true);
            fatherPriMethod.invoke(user);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    /**
     * @Author WLJ
     * @Description //TODO 获取构造器并执行
     * @Date 17:08 2023/8/29
     * @Param []
     * @return void
     **/
    private static void method4() {
        Class clazz = getClazz();
        try {
            // 公共无参构造器
            Constructor constructor = clazz.getConstructor();
            constructor.newInstance();
            // 公共有参构造器
            Constructor constructor1 = clazz.getConstructor(int.class, String.class);
            constructor1.newInstance(1, "1");
            // 私有有参构造器
            Constructor constructor2 = clazz.getDeclaredConstructor(int.class);
            constructor2.setAccessible(true);
            constructor2.newInstance(2);
        } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    /**
     * @Author WLJ
     * @Description //TODO 获取class对象
     * @Date 14:49 2023/8/29
     * @Param []
     * @return java.lang.Class
     **/
    private static Class getClazz() {
        try {
            return Class.forName("reflex_and_annotation.entity.User");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
